﻿using System;
using System.Collections;
using System.Windows.Forms;
using log4net;
using MPMovieCatalog.Util;

namespace MPMovieCatalog.Configuration.Sections
{
    public partial class ScriptSectionForm : ConfigurationFormSection
    {
        private static readonly ILog log = LogManager.GetLogger(typeof(ScriptSectionForm));

        private ScriptSection m_scriptSection;

        public ScriptSectionForm(ScriptSection scriptSection)
            : this(ResourceUtils.GetString("options", 6))
        {
            m_scriptSection = scriptSection;
        }

        public ScriptSectionForm(string text)
            : base(text)
        {
            InitializeComponent();
            UpdateUI();
            this.Load += new EventHandler(ScriptSectionForm_Load);
        }

        private void ScriptSectionForm_Load(object sender, EventArgs e)
        {
            availScriptsComboBox.Items.AddRange(FetcherUtils.FindAllScripts().ToArray());

            Binding bindingScripts = new Binding("Scripts", m_scriptSection,
                        "Scripts", true, DataSourceUpdateMode.OnPropertyChanged);
            this.DataBindings.Add(bindingScripts);
        }

        public ArrayList Scripts
        {
            get
            {
                ArrayList scriptFiles = new ArrayList();
                foreach (ListViewItem item in scriptsListView.Items)
                {
                    ScriptFile scriptFile = new ScriptFile();
                    scriptFile.FileName = item.Name;
                    scriptFile.Name = item.SubItems[0].Text;
                    scriptFile.Language = item.SubItems[1].Text;
                    scriptFile.Limit = int.Parse(item.SubItems[2].Text);

                    scriptFiles.Add(scriptFile);
                }
                return scriptFiles;
            }

            set
            {
                scriptsListView.Items.Clear();
                foreach (ScriptFile scriptFile in value)
                {
                    AddScriptFile(scriptFile);
                }
            }
        }

        private void addScriptButton_Click(object sender, EventArgs e)
        {
            AddScriptFile((ScriptFile)availScriptsComboBox.SelectedItem);
        }

        private void AddScriptFile(ScriptFile scriptFile)
        {
            if (scriptFile != null && scriptsListView.Items.IndexOfKey(scriptFile.FileName) < 0)
            {
                string[] arr = new string[3];
                arr[0] = scriptFile.Name;
                arr[1] = scriptFile.Language;
                arr[2] = scriptFile.Limit.ToString();

                ListViewItem item = new ListViewItem(arr);
                item.Name = scriptFile.FileName;

                scriptsListView.Items.Add(item);
            }
        }

        private void deleteScriptButton_Click(object sender, EventArgs e)
        {
            if (scriptsListView.SelectedItems != null && scriptsListView.SelectedItems.Count > 0)
            {
                scriptsListView.Items.Remove(scriptsListView.SelectedItems[0]);
            }
        }

        private void upScriptButton_Click(object sender, EventArgs e)
        {
            if (scriptsListView.SelectedIndices != null && scriptsListView.SelectedIndices.Count > 0 && scriptsListView.SelectedIndices[0] > 0)
            {
                int selectedIndex = scriptsListView.SelectedIndices[0];
                ListViewItem selectedItem = scriptsListView.Items[selectedIndex];
                scriptsListView.Items.RemoveAt(selectedIndex);
                scriptsListView.Items.Insert(selectedIndex - 1, selectedItem);
            }
        }

        private void downScriptButton_Click(object sender, EventArgs e)
        {
            if (scriptsListView.SelectedIndices != null && scriptsListView.SelectedIndices.Count > 0 && scriptsListView.SelectedIndices[0] < scriptsListView.Items.Count - 1)
            {
                int selectedIndex = scriptsListView.SelectedIndices[0];
                ListViewItem selectedItem = scriptsListView.Items[selectedIndex];
                scriptsListView.Items.RemoveAt(selectedIndex);
                scriptsListView.Items.Insert(selectedIndex + 1, selectedItem);
            }
        }

        private void UpdateUI()
        {
            nameColumnHeader.Text = ResourceUtils.GetString("options", 7);
            languageColumnHeader.Text = ResourceUtils.GetString("options", 8);
            limitColumnHeader.Text = ResourceUtils.GetString("options", 9);
        }
    }

    public class ScriptFile
    {
        public string Name { get; set; }
        public string Language { get; set; }
        public int Limit { get; set; }
        public string FileName { get; set; }

        public override string ToString()
        {
            return string.Format("{0} ({1} - {2})", FileName, Name, Language);
        }
    }

}
